<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('student.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.accountId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.studentName')" prop="studentName">
        <el-input
          v-model="formData.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studentName')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.recruitType')" prop="recruitType">
        <el-select
          v-model="formData.recruitType"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.recruitType')"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.phone')" prop="phone">
        <el-input
          v-model="formData.phone"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.phone')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.batchId')" prop="batchId">
        <el-input
          v-model="formData.batchId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.batchId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.recruitStatus')" prop="recruitStatus">
        <el-radio-group v-model="formData.recruitStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.recruitTime')" prop="recruitTime">
        <el-date-picker
          v-model="formData.recruitTime"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.recruitTime')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewQuality')" prop="interviewQuality">
        <el-input
          v-model="formData.interviewQuality"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewQuality')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewSelected')" prop="interviewSelected">
        <el-radio-group v-model="formData.interviewSelected">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewDate')" prop="interviewDate">
        <el-date-picker
          v-model="formData.interviewDate"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewDate')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewTime')" prop="interviewTime">
        <el-date-picker
          v-model="formData.interviewTime"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewTime')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewStatus')" prop="interviewStatus">
        <el-radio-group v-model="formData.interviewStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewId')" prop="interviewId">
        <el-input
          v-model="formData.interviewId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewGroupId')" prop="interviewGroupId">
        <el-input
          v-model="formData.interviewGroupId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewGroupId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewSlotId')" prop="interviewSlotId">
        <el-input
          v-model="formData.interviewSlotId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewSlotId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewSlotTime')" prop="interviewSlotTime">
        <el-input
          v-model="formData.interviewSlotTime"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewSlotTime')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewScore')" prop="interviewScore">
        <el-input
          v-model="formData.interviewScore"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.interviewScore')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.chioceStatus')" prop="chioceStatus">
        <el-radio-group v-model="formData.chioceStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.chioceMentorId')" prop="chioceMentorId">
        <el-input
          v-model="formData.chioceMentorId"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.chioceMentorId')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.birthdate')" prop="birthdate">
        <el-date-picker
          v-model="formData.birthdate"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.birthdate')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.gender')" prop="gender">
        <el-input
          v-model="formData.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.gender')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.politicalStatus')" prop="politicalStatus">
        <el-radio-group v-model="formData.politicalStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.ethnic')" prop="ethnic">
        <el-input
          v-model="formData.ethnic"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.ethnic')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.identityNumber')" prop="identityNumber">
        <el-input
          v-model="formData.identityNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.identityNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.contactAddress')" prop="contactAddress">
        <el-input
          v-model="formData.contactAddress"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.contactAddress')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.email')" prop="email">
        <el-input
          v-model="formData.email"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.email')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.university')" prop="university">
        <el-input
          v-model="formData.university"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.university')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.is985')" prop="is985">
        <el-radio-group v-model="formData.is985">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.is211')" prop="is211">
        <el-radio-group v-model="formData.is211">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.isFirstClass')" prop="isFirstClass">
        <el-radio-group v-model="formData.isFirstClass">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('student.fields.studyMajor')" prop="studyMajor">
        <el-input
          v-model="formData.studyMajor"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studyMajor')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.applyMajor')" prop="applyMajor">
        <el-input
          v-model="formData.applyMajor"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.applyMajor')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.majorRank')" prop="majorRank">
        <el-input
          v-model="formData.majorRank"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.majorRank')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.majorTotal')" prop="majorTotal">
        <el-input
          v-model="formData.majorTotal"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.majorTotal')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.classRank')" prop="classRank">
        <el-input
          v-model="formData.classRank"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.classRank')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.classTotal')" prop="classTotal">
        <el-input
          v-model="formData.classTotal"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.classTotal')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.cet4Score')" prop="cet4Score">
        <el-input
          v-model="formData.cet4Score"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.cet4Score')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.cet6Score')" prop="cet6Score">
        <el-input
          v-model="formData.cet6Score"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.cet6Score')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.attachment')" prop="attachment">
        <el-input
          v-model="formData.attachment"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.attachment')"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.remark')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{
        t('common.ok')
      }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { StudentApi, StudentVO } from '@/api/recruit/recruitstudent'
import { FormRules } from 'element-plus'

/** 报名学生 表单 */
defineOptions({ name: 'StudentForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  studentName: undefined,
  recruitType: undefined,
  phone: undefined,
  batchId: undefined,
  recruitStatus: undefined,
  recruitTime: undefined,
  interviewQuality: undefined,
  interviewSelected: undefined,
  interviewDate: undefined,
  interviewTime: undefined,
  interviewStatus: undefined,
  interviewId: undefined,
  interviewGroupId: undefined,
  interviewSlotId: undefined,
  interviewSlotTime: undefined,
  interviewScore: undefined,
  chioceStatus: undefined,
  chioceMentorId: undefined,
  birthdate: undefined,
  gender: undefined,
  politicalStatus: undefined,
  ethnic: undefined,
  identityNumber: undefined,
  contactAddress: undefined,
  email: undefined,
  university: undefined,
  is985: undefined,
  is211: undefined,
  isFirstClass: undefined,
  studyMajor: undefined,
  applyMajor: undefined,
  majorRank: undefined,
  majorTotal: undefined,
  classRank: undefined,
  classTotal: undefined,
  cet4Score: undefined,
  cet6Score: undefined,
  attachment: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.accountId'),
      trigger: 'blur'
    }
  ],
  studentName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.studentName'),
      trigger: 'blur'
    }
  ],
  recruitType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.recruitType'),
      trigger: 'change'
    }
  ],
  phone: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.phone'),
      trigger: 'blur'
    }
  ],
  batchId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.batchId'),
      trigger: 'blur'
    }
  ],
  recruitStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.recruitStatus'),
      trigger: 'blur'
    }
  ],
  recruitTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.recruitTime'),
      trigger: 'blur'
    }
  ],
  interviewQuality: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewQuality'),
      trigger: 'blur'
    }
  ],
  interviewSelected: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewSelected'),
      trigger: 'blur'
    }
  ],
  interviewDate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewDate'),
      trigger: 'blur'
    }
  ],
  interviewTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewTime'),
      trigger: 'blur'
    }
  ],
  interviewStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewStatus'),
      trigger: 'blur'
    }
  ],
  interviewId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewId'),
      trigger: 'blur'
    }
  ],
  interviewGroupId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewGroupId'),
      trigger: 'blur'
    }
  ],
  interviewSlotId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewSlotId'),
      trigger: 'blur'
    }
  ],
  interviewSlotTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewSlotTime'),
      trigger: 'blur'
    }
  ],
  interviewScore: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.interviewScore'),
      trigger: 'blur'
    }
  ],
  chioceStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.chioceStatus'),
      trigger: 'blur'
    }
  ],
  chioceMentorId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.chioceMentorId'),
      trigger: 'blur'
    }
  ],
  birthdate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.birthdate'),
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.gender'),
      trigger: 'blur'
    }
  ],
  politicalStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.politicalStatus'),
      trigger: 'blur'
    }
  ],
  ethnic: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.ethnic'),
      trigger: 'blur'
    }
  ],
  identityNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.identityNumber'),
      trigger: 'blur'
    }
  ],
  contactAddress: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.contactAddress'),
      trigger: 'blur'
    }
  ],
  email: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.email'),
      trigger: 'blur'
    }
  ],
  university: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.university'),
      trigger: 'blur'
    }
  ],
  is985: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.is985'),
      trigger: 'blur'
    }
  ],
  is211: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.is211'),
      trigger: 'blur'
    }
  ],
  isFirstClass: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.isFirstClass'),
      trigger: 'blur'
    }
  ],
  studyMajor: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.studyMajor'),
      trigger: 'blur'
    }
  ],
  applyMajor: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.applyMajor'),
      trigger: 'blur'
    }
  ],
  majorRank: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.majorRank'),
      trigger: 'blur'
    }
  ],
  majorTotal: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.majorTotal'),
      trigger: 'blur'
    }
  ],
  classRank: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.classRank'),
      trigger: 'blur'
    }
  ],
  classTotal: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.classTotal'),
      trigger: 'blur'
    }
  ],
  cet4Score: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.cet4Score'),
      trigger: 'blur'
    }
  ],
  cet6Score: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.cet6Score'),
      trigger: 'blur'
    }
  ],
  attachment: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.attachment'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.remark'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('student.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StudentApi.getStudent(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as StudentVO
    if (formType.value === 'create') {
      await StudentApi.createStudent(data)
      message.success(t('common.createSuccess'))
    } else {
      await StudentApi.updateStudent(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    studentName: undefined,
    recruitType: undefined,
    phone: undefined,
    batchId: undefined,
    recruitStatus: undefined,
    recruitTime: undefined,
    interviewQuality: undefined,
    interviewSelected: undefined,
    interviewDate: undefined,
    interviewTime: undefined,
    interviewStatus: undefined,
    interviewId: undefined,
    interviewGroupId: undefined,
    interviewSlotId: undefined,
    interviewSlotTime: undefined,
    interviewScore: undefined,
    chioceStatus: undefined,
    chioceMentorId: undefined,
    birthdate: undefined,
    gender: undefined,
    politicalStatus: undefined,
    ethnic: undefined,
    identityNumber: undefined,
    contactAddress: undefined,
    email: undefined,
    university: undefined,
    is985: undefined,
    is211: undefined,
    isFirstClass: undefined,
    studyMajor: undefined,
    applyMajor: undefined,
    majorRank: undefined,
    majorTotal: undefined,
    classRank: undefined,
    classTotal: undefined,
    cet4Score: undefined,
    cet6Score: undefined,
    attachment: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
